package buildrobot_info

import (
	"go_server/game_server/api/service"
	"go_server/game_server/event_manager"
	"go_server/game_server/event_manager/event_data"
	"go_server/game_server/world_manager"
	"go_server/zinx/zlog"
	"go_server/zinx/ztimer"
	"time"
)

type Buildrobot_info_service struct {
	service.AbsService
	eventMethods                   map[event_data.EventType]func(event_data.IEvent)
	Router_AddBuildRobotLvUp       *Router_AddBuildRobotLvUp
	Router_DelBuildRobotLvUp       *Router_DelBuildRobotLvUp
	Router_BuildRobotLvUpOver      *Router_BuildRobotLvUpOver
	Router_DelAllRobot2BuildLvUp   *Router_DelAllRobot2BuildLvUp
	Router_GetAllBuildRobots       *Router_GetAllBuildRobots
	Router_BuildRobotUpgradeByItem *Router_BuildRobotUpgradeByItem
	Router_BuildRobotChargeByItem  *Router_BuildRobotChargeByItem
	IsUseRedis                     bool //是否使用Redis
}

var (
	Buildrobot_info_serviceObj *Buildrobot_info_service
)

func (s *Buildrobot_info_service) GetModuleName() string {
	return "buildrobot_info"
}
func (s *Buildrobot_info_service) GetEventMap() map[event_data.EventType]func(event_data.IEvent) {
	return s.eventMethods
}
func (s *Buildrobot_info_service) GetEventHandleFunc(eventType event_data.EventType) (func(event_data.IEvent), bool) {
	eventFunc, ok := s.eventMethods[eventType]
	if !ok {
		zlog.Errorf("GetEventHandleFunc Err!!! method not found!! eventType: %d", eventType)
		return nil, false
	}
	return eventFunc, true
}

func init() {
	Buildrobot_info_serviceObj = &Buildrobot_info_service{
		Router_AddBuildRobotLvUp:       &Router_AddBuildRobotLvUp{},
		Router_DelBuildRobotLvUp:       &Router_DelBuildRobotLvUp{},
		Router_BuildRobotLvUpOver:      &Router_BuildRobotLvUpOver{},
		Router_DelAllRobot2BuildLvUp:   &Router_DelAllRobot2BuildLvUp{},
		Router_GetAllBuildRobots:       &Router_GetAllBuildRobots{},
		Router_BuildRobotUpgradeByItem: &Router_BuildRobotUpgradeByItem{},
		Router_BuildRobotChargeByItem:  &Router_BuildRobotChargeByItem{},
		eventMethods:                   make(map[event_data.EventType]func(event_data.IEvent)),

		//--分隔线-----------------------------------------------------------------------------
		IsUseRedis: false,
	}
	event_manager.EventManagerObj.AddServiceEvent(event_data.RoleCreateEvent, onReceiveRoleCreateEvent_buildrobot_info, Buildrobot_info_serviceObj)
	event_manager.EventManagerObj.AddServiceEvent(event_data.LoginEvent, OnLoginEvent_buildrobot_info, Buildrobot_info_serviceObj)
	event_manager.EventManagerObj.AddServiceEvent(event_data.BuildUnlockEvent, BuildUnlockEvent_buildrobot_info, Buildrobot_info_serviceObj)
}

// 创建角色完成事件
func onReceiveRoleCreateEvent_buildrobot_info(event event_data.IEvent) {
	conn := event.GetRoleConn()
	roleId, ok := event.GetRoleId()
	if !ok {
		return
	}
	Buildrobot_info_serviceObj.S2C_CheckBuildRobotInfoByBuildUnlock(conn, roleId)
}

// 建筑初始化完成事件
func OnLoginEvent_buildrobot_info(event event_data.IEvent) {
	conn := event.GetRoleConn()
	roleId, ok := event.GetRoleId()
	if !ok {
		return
	}
	Buildrobot_info_serviceObj.S2C_CheckBuildRobotInfoByBuildUnlock(conn, roleId)
	Buildrobot_info_serviceObj.S2C_GetAllBuildRobots(conn, roleId, nil)
}

// 建筑解锁事件
func BuildUnlockEvent_buildrobot_info(event event_data.IEvent) {
	conn := event.GetRoleConn()
	roleId, ok := event.GetRoleId()
	if !ok {
		return
	}
	Buildrobot_info_serviceObj.S2C_CheckBuildRobotInfoByBuildUnlock(conn, roleId)
	Buildrobot_info_serviceObj.S2C_GetAllBuildRobots(conn, roleId, nil)
}

func (s *Buildrobot_info_service) AddTimerCheck(build *Cache_buildrobot_info_build_data) {
	zlog.Info("建筑升级倒计时 roleid: %v", build.Build_info_id)
	ztimer.NewTimerAfter(ztimer.NewDelayFunc(onCheckBuildEndtime, []interface{}{build.Build_info_roleId, build.EndMilliTime}),
		time.Duration(build.UpgradeTime)*time.Second).Run()
}

func onCheckBuildEndtime(v ...interface{}) {
	roleid := v[0].(int64)
	zlog.Info("时间检测 roleid: %v", roleid)
	conn := world_manager.WorldMgrObj.GetPlayerConn(roleid)
	if conn != nil {
		Buildrobot_info_serviceObj.S2C_BuildRobotLvUpOver(conn, roleid)
	}
}
